/**
 * 
 */
package org.gov.vis.data.gui.listener;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.ResourceBundle;

import javax.swing.JOptionPane;

import org.gov.vis.data.dto.FollowUpDetailsDto;
import org.gov.vis.data.gui.EditInsertFollowUpDetailsDialog;
import org.gov.vis.data.gui.EditProgencyDialog;
import org.gov.vis.master.gui.listener.BaseListener;
import org.gov.vis.util.DBConstants;
import org.gov.vis.util.DU;
import org.gov.vis.util.Util;
import org.gov.vis.util.ValidatorUtils;

/**
 * @author NAGESH
 *
 */
public class EditInsertFollowUpDetailsListener implements ActionListener,BaseListener {


	List<FollowUpDetailsDto> totalLst;
	private EditInsertFollowUpDetailsDialog followUpDetailsDialog;
	private FollowUpDetailsDto addFollowUpDetailsDto;
	//public static List<FollowUpDetailsDto> tempFollowUpLst = new ArrayList<FollowUpDetailsDto>();
	public HashMap<String, String> errorMsgs = new HashMap<String, String>();
	public ResourceBundle rb = Util.getResources();

	List<FollowUpDetailsDto> followUpLst = EditProgencyDialog.tempFollowUpLst;
	private boolean errorsFound = false;

	public EditInsertFollowUpDetailsListener(EditInsertFollowUpDetailsDialog followUpDetailsDialog){

		this.followUpDetailsDialog = followUpDetailsDialog;
		followUpLst = new ArrayList<FollowUpDetailsDto>();
	}


	@Override
	public void actionPerformed(ActionEvent actionEvent) {
		// TODO Auto-generated method stub

		if(actionEvent.getActionCommand().equals("Save")){
			errorsFound = false;
			addFollowUpDetails();
			if(!errorsFound){
				followUpLst.add(addFollowUpDetailsDto);
				totalLst = followUpDetailsDialog.getTmpFollowUpLst();
				if(totalLst == null){
					totalLst = new ArrayList<FollowUpDetailsDto>();
				}
				totalLst.addAll(followUpLst);
				followUpDetailsDialog.setTmpFollowUpLst(totalLst);

				followUpDetailsDialog.dispose();
				EditProgencyDialog editProgencyDialog = (EditProgencyDialog)followUpDetailsDialog.getProgencyObject();
				editProgencyDialog.setFollowUpLst(totalLst);
				editProgencyDialog.refreshScreen(totalLst);
			}
		}else if (actionEvent.getActionCommand().equals("Reset")){
			clear();
		}

	}

	private void addFollowUpDetails() {
		// TODO Auto-generated method stub
		setGuiDetailsToDto(followUpDetailsDialog);

		//need to perform validations and save the FollowUpDetails
	}

	@Override
	public void clear() {
		// TODO Auto-generated method stub
		followUpDetailsDialog.getTxtBxBirthWeight().setText("");
		followUpDetailsDialog.getDtChoDateOfDeticking().setDate(null);
		followUpDetailsDialog.getDtChoDateOfDeworming().setDate(null);
		followUpDetailsDialog.getDtChoDateOfVaccination().setDate(null);
		followUpDetailsDialog.getTxtBxNutrition().setText(null);
		followUpDetailsDialog.getTxtBxOthers().setText(null);
		followUpDetailsDialog.getTxtBxMonthlyWt().setText(null);
		//Labels to default Black color
		clearGUILabelColors(blackColor);


	}

	@Override
	public void setGuiDetailsToDto(Object _guiObject) {
		final EditInsertFollowUpDetailsDialog _editFollowUpDialog = (EditInsertFollowUpDetailsDialog)_guiObject;
		addFollowUpDetailsDto = new FollowUpDetailsDto();
		if(_editFollowUpDialog.getDtChoDateOfDeworming().getDate() != null){
			addFollowUpDetailsDto.setDeWormingDate(_editFollowUpDialog.getDtChoDateOfDeworming().getDate());
		}
		if(_editFollowUpDialog.getDtChoDateOfDeticking().getDate() != null){
			addFollowUpDetailsDto.setDeTickingDate(_editFollowUpDialog.getDtChoDateOfDeticking().getDate());
		}
		if(_editFollowUpDialog.getDtChoDateOfVaccination().getDate() != null){
			addFollowUpDetailsDto.setVaccinationDate(_editFollowUpDialog.getDtChoDateOfVaccination().getDate());
		}
		addFollowUpDetailsDto.setOther(_editFollowUpDialog.getTxtBxOthers().getText());
		addFollowUpDetailsDto.setNutrition(_editFollowUpDialog.getTxtBxBirthWeight().getText());
		addFollowUpDetailsDto.setBirthWeight(_editFollowUpDialog.getTxtBxBirthWeight().getText());
		addFollowUpDetailsDto.setMonthlyWeight(_editFollowUpDialog.getTxtBxMonthlyWt().getText());
		validate(addFollowUpDetailsDto);
		//EditProgencyDialog.tempFollowUpLst.add(addFollowUpDetailsDto);
		//followUpLst.add(addFollowUpDetailsDto);
		//tempFollowUpLst.add(addFollowUpDetailsDto);
		//ProgencyDto progencyDto = new ProgencyDto();
		/*		followUpLst.add(addFollowUpDetailsDto);
		 totalLst = _editFollowUpDialog.getTmpFollowUpLst();

		totalLst.addAll(followUpLst);
		_editFollowUpDialog.setTmpFollowUpLst(totalLst);
		 */

		//EditProgencyDialog editProgencyDialog = new EditProgencyDialog();
		//System.out.println(editProgencyDialog.getFollowUpLst());
		//editProgencyDialog.getFollowUpLst().addAll(EditProgencyDialog.tempFollowUpLst);

	}

	private void validate(FollowUpDetailsDto addFollowUpDetailsDto2) {

		try{
			String dewromingDateErr = ValidatorUtils.validateString(rb.getString("dialog_label_prgy_dworing"), DU.format(addFollowUpDetailsDto2.getDeWormingDate(), DBConstants.DISPLAY_DATE_FORMAT), 6, 50, true);
			if(dewromingDateErr != null)
				errorMsgs.put(rb.getString("dialog_label_prgy_dworing").toUpperCase(), dewromingDateErr);
			String detickinggDateErr = ValidatorUtils.validateString(rb.getString("dialog_label_prgy_deticking"), DU.format(addFollowUpDetailsDto2.getDeWormingDate(), DBConstants.DISPLAY_DATE_FORMAT), 6, 50, true);
			if(detickinggDateErr != null)
				errorMsgs.put(rb.getString("dialog_label_prgy_deticking").toUpperCase(), detickinggDateErr);

			String vaccinationDateErr = ValidatorUtils.validateString(rb.getString("dialog_label_prgy_vaccintion"), DU.format(addFollowUpDetailsDto2.getDeWormingDate(), DBConstants.DISPLAY_DATE_FORMAT), 6, 50, true);
			if(vaccinationDateErr != null)
				errorMsgs.put(rb.getString("dialog_label_prgy_vaccintion").toUpperCase(), vaccinationDateErr);
			String monthWeightErr = ValidatorUtils.validateDecimal(rb.getString("dialog_label_prgy_grwmnthwgt"), addFollowUpDetailsDto2.getMonthlyWeight(), ValidatorUtils.DECIMALS, 0, Double.MAX_VALUE, 0, 2, false, false);
			if(monthWeightErr != null)
				errorMsgs.put(rb.getString("dialog_label_prgy_grwmnthwgt").toUpperCase(), monthWeightErr);
			String birthWeightErr = ValidatorUtils.validateDecimal(rb.getString("dialog_label_prgy_grwbrthwgt"), addFollowUpDetailsDto2.getBirthWeight(), ValidatorUtils.DECIMALS, 0, Double.MAX_VALUE, 0, 2, false, false);
			if(birthWeightErr != null)
				errorMsgs.put(rb.getString("dialog_label_prgy_grwbrthwgt").toUpperCase(), birthWeightErr);

			if(errorMsgs != null && !errorMsgs.isEmpty()){
				throw new Exception();
			}
		}catch (Exception e) {
			String errMsg = null;
			List<String> errorMsgLst = new ArrayList<String>();
			if(errorMsgs.containsKey(rb.getString("dialog_label_prgy_dworing").toUpperCase())){
				followUpDetailsDialog.getLblDeworing().setForeground(redColor);
				errMsg = errorMsgs.remove(rb.getString("dialog_label_prgy_dworing").toUpperCase());
				errorMsgLst.add(errMsg);
			}
			if(errorMsgs.containsKey(rb.getString("dialog_label_prgy_vaccintion").toUpperCase())){
				followUpDetailsDialog.getLblVaccinations().setForeground(redColor);
				errMsg = errorMsgs.remove(rb.getString("dialog_label_prgy_vaccintion").toUpperCase());
				errorMsgLst.add(errMsg);
			}
			if(errorMsgs.containsKey(rb.getString("dialog_label_prgy_deticking").toUpperCase())){
				followUpDetailsDialog.getLblDeticking().setForeground(redColor);
				errMsg = errorMsgs.remove(rb.getString("dialog_label_prgy_deticking").toUpperCase());
				errorMsgLst.add(errMsg);
			}
			if(errorMsgs.containsKey(rb.getString("dialog_label_prgy_grwbrthwgt").toUpperCase())){
				followUpDetailsDialog.getLblBirthWt().setForeground(redColor);
				errMsg = errorMsgs.remove(rb.getString("dialog_label_prgy_grwbrthwgt").toUpperCase());
				errorMsgLst.add(errMsg);
			}
			if(errorMsgs.containsKey(rb.getString("dialog_label_prgy_grwmnthwgt").toUpperCase())){
				followUpDetailsDialog.getLblMonthlyWt().setForeground(redColor);
				errMsg = errorMsgs.remove(rb.getString("dialog_label_prgy_grwmnthwgt").toUpperCase());
				errorMsgLst.add(errMsg);
			}
			String errorMessages = "";
			Iterator<String> itr = errorMsgLst.iterator();
			while(itr.hasNext()){
				errMsg = itr.next();
				errorMessages += errMsg;

				if(!errMsg.contains("\n")){
					errorMessages += "\n";
				}
			}
			JOptionPane.showMessageDialog(followUpDetailsDialog, errorMessages);
			if(!errorMessages.equals("")){
				errorsFound  = true;
			}
		}
	}


	@Override
	public void setDtoDetailsToGui(Object _dtoObject) {
		// TODO Auto-generated method stub

	}


	@Override
	public void clearGUILabelColors(Color color) {
		followUpDetailsDialog.getLblBirthWt().setForeground(color);
		followUpDetailsDialog.getLblDeticking().setForeground(color);
		followUpDetailsDialog.getLblDeworing().setForeground(color);
		followUpDetailsDialog.getLblMonthlyWt().setForeground(color);
		followUpDetailsDialog.getLblNutrition().setForeground(color);
		followUpDetailsDialog.getLblOthers().setForeground(color);
		followUpDetailsDialog.getLblVaccinations().setForeground(color);

	}


}
